package com.frxuexi.visit.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitApproveBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitApproveActivity")
@AndroidEntryPoint
class VisitApproveActivity :
    BaseActivity<ActivityVisitApproveBinding, VisitIntent, VisitViewModel>() {
    override val binding: ActivityVisitApproveBinding by lazy {
        ActivityVisitApproveBinding.inflate(
            layoutInflater
        )
    }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()

    @Autowired
    lateinit var vid: String
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
                                val item = it.data as VisitBeanItem
                                binding.bean = item
                                if (item.visitStatus == "待审核") {
                                    binding.visitDetailGroup.visibility = View.VISIBLE
                                }
                            }

                            RequestType.UPDATE -> {
                                ToastUtils.showLong("审核完成")
                                finish()
                            }

                            else -> {}
                        }


                    }

                    is UiState.onError -> {
                        Log.i("lr", it.msg)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(VisitIntent.getVisitById(vid.toInt()))


        binding.visitDetailOk.setOnClickListener {
            vm.sendIntent(VisitIntent.approveVisit(vid.toInt()))
        }

        binding.visitDetailRefause.setOnClickListener {
            vm.sendIntent(VisitIntent.rejectVisit(vid.toInt()))
        }
    }

}